package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.entity.PlantCategory;
import com.example.exception.FloraException;
import com.example.mapper.PlantCategoryMapper;
import com.example.service.IPlantCategoryService;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zrr
 * @since 2022-12-06
 */
@Service
public class PlantCategoryServiceImpl extends ServiceImpl<PlantCategoryMapper, PlantCategory> implements IPlantCategoryService {

    @Override
    public PlantCategory addPlantCategory(PlantCategory plantCategory) throws FloraException {
        try{
            LambdaQueryWrapper<PlantCategory> lqw=new LambdaQueryWrapper<>();
            lqw.eq(PlantCategory::getPlantCategoryName,plantCategory.getPlantCategoryName());
            if(this.baseMapper.selectOne(lqw)!=null){
                throw new FloraException("植物分类"+plantCategory.getPlantCategoryName()+"已存在");
            }else{
                this.baseMapper.insert(plantCategory);
            }
            return plantCategory;
        }catch (Exception e){
            throw new FloraException("添加植物分类失败"+e.getMessage());
        }
    }

    @Override
    public PlantCategory updatePlantCategory(PlantCategory plantCategory) throws FloraException {
        try{
            if(this.baseMapper.selectById(plantCategory.getPlantCategoryId())==null){
                throw new FloraException("植物分类"+plantCategory.getPlantCategoryId()+"不存在");
            }
            plantCategory.setPlantNum(null);
            this.baseMapper.updateById(plantCategory);
            return this.baseMapper.selectById(plantCategory.getPlantCategoryId());
        }catch (Exception e){
            throw new FloraException("更新植物分类失败"+e.getMessage());
        }
    }

    @Override
    public PlantCategory getPlantCategoryById(Long id) {
        System.out.println("service:"+baseMapper.selectById(id));
        return baseMapper.selectById(id);
    }
}
